package com.itheima.em.server.service.impl.tianditu;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.em.config.AMapServerConfig;
import com.itheima.em.config.EagleConfig;
import com.itheima.em.config.TMapServerConfig;
import com.itheima.em.enums.ProviderType;
import com.itheima.em.mapper.TraceServerMapper;
import com.itheima.em.pojo.TraceServer;
import com.itheima.em.server.config.MybatisPlusConfig;
import com.itheima.em.server.service.impl.amap.AMapWebApiService;
import com.itheima.em.service.EagleOrdered;
import com.itheima.em.service.TraceServerService;
import com.itheima.em.util.SnowflakeIdGenerator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 天地图的轨迹（服务）实现类
 */
@Service("TMapTraceServerService")
@ConditionalOnBean({TMapServerConfig.class, MybatisPlusConfig.class})
public class TMapTraceServerServiceImpl extends ServiceImpl<TraceServerMapper, TraceServer> implements TraceServerService {

    /**
     * 雪花ID生成器
     */
    public SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(this.getClass().hashCode()%31, this.getProvider().getValue()); // 创建雪花ID生成器，传入机器ID和数据中心ID

    @Resource
    private EagleConfig eagleConfig;

    @Resource
    private TMapWebApiService tMapWebApiService;

    /**
     * 创建服务，
     *
     * @param name 服务名称
     * @param desc 服务描述，非必须
     * @return
     */
    @Override
    public String create(String name, String desc) {
        //获取服务ID
        Long serverId = idGenerator.generateId();

        //将服务数据存储到数据库
        TraceServer traceServer = new TraceServer();
        traceServer.setProvider(ProviderType.TMAP);
        traceServer.setName(name);
        traceServer.setDesc(desc);
        traceServer.setStatus(true);
        traceServer.setServerId(serverId);

        super.save(traceServer);

        return Convert.toStr(traceServer.getServerId());

    }

    @Override
    public String delete(Long serverId) {
        //删除数据库中的数据
        LambdaQueryWrapper<TraceServer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TraceServer::getServerId, serverId);
        queryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);
        return super.remove(queryWrapper) ? null : "err";
    }

    @Override
    public String update(Long serverId, String name, String desc) {
        //更新数据库中的数据
        TraceServer traceServer = new TraceServer();
        traceServer.setName(name);
        traceServer.setDesc(desc);

        LambdaQueryWrapper<TraceServer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TraceServer::getServerId, serverId);
        queryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);

        return super.update(traceServer, queryWrapper) ? null : "err";
    }

    @Override
    public List<TraceServer> queryAll() {
        LambdaQueryWrapper<TraceServer> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public TraceServer queryByServerId(Long serverId) {
        LambdaQueryWrapper<TraceServer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TraceServer::getServerId, serverId);
        queryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);
        return super.getOne(queryWrapper);
    }



    @Override
    public TraceServer queryAvailableServer() {
        LambdaQueryWrapper<TraceServer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TraceServer::getStatus, true);
        queryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);

        List<TraceServer> serverList = super.list(queryWrapper);
        if (CollUtil.isNotEmpty(serverList)) {
            return serverList.get(0);
        }
        return null;
    }

    @Override
    public Boolean markNotAvailable(Long serverId) {
        LambdaQueryWrapper<TraceServer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TraceServer::getServerId, serverId);
        queryWrapper.eq(TraceServer::getProvider, ProviderType.TMAP);

        TraceServer traceServer = new TraceServer();
        traceServer.setStatus(false);
        return super.update(traceServer, queryWrapper);
    }

    @Override
    public int getOrder() {
        return EagleOrdered.ONE;
    }
    @Override
    public ProviderType getProvider() {
        return ProviderType.TMAP;
    }
}
